package com.bdqn.t320.ch12.repository;

import com.bdqn.t320.ch12.entity.User;
import com.bdqn.t320.ch12.entity.UserInfo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Map;

public interface UserRepository extends JpaRepository<User, Long>, JpaSpecificationExecutor<User> {

    public List<User> findByUsrName(String usrName);

    public User findUserByUsrName(String usrName);

    public User findByUsrNameAndUsrPassword(String usrName, String usrPassword);

//    @Query(value="select u from User u where u.usrRoleId=?1",nativeQuery = false)
//    public List<User> findByaaRoleId(Long roleId);
//
//    @Query(value = "select * from sys_user where usr_role_id=?1",nativeQuery = true)
//    public List<User> findByRoleId(Long roleId);
//
//    @Query("select u from User u where u.usrRoleId=?1")
//    public Page<User> findPageByUsrRoleId(Long roleId, Pageable pageable);

    // public Page<User> findPageByMap(Map param, Pageable pageable);

//    @Query("select u.usrId as usrId, u.usrName as usrName, u.usrPassword as usrPassword, " +
//            "u.usrRoleId as usrRoleId, u.usrFlag as usrFlag, r.roleName as roleName " +
//            "from User u, Role r where u.usrRoleId=r.roleId and u.usrId=?1")
//    public UserInfo getUserInfo(Long usrId);

    @Query(value = "select rf_right_code from sys_role_right where rf_role_id = (select usr_role_id from sys_user where usr_id = ?1)", nativeQuery = true)
    public List<String> getRfRightCode(Long usrId);

    @Query(value = "select u.usr_id,u.usr_name,u.usr_password,u.usr_role_id,u.usr_flag,r.role_name from sys_user u,sys_role r  where u.usr_role_id =r.role_id order by usr_id desc", nativeQuery = true)
    public List<Map<String, Object>> getUserAll();


}
